﻿namespace Pap.Web.Modules.Membership.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using Microsoft.Practices.ServiceLocation;
    using Pap.Data;
    using Pap.Data.Abstraction.Utils;
    using Pap.Web.Utils;

    /// <summary>
    /// THe ViewModel for Role Details
    /// </summary>
    public class RoleDetailsViewModel : IViewModel
    {
        /// <summary>
        /// local variable for membership repository
        /// </summary>
        private IMembershipRepository _MembershipRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="RoleDetailsViewModel"/> class.
        /// </summary>
        /// <param name="membershipRepository">The membership repository.</param>
        public RoleDetailsViewModel(IMembershipRepository membershipRepository)
        {
            this._MembershipRepository = membershipRepository;
        }

        /// <summary>
        /// Doeses the role name exist.
        /// </summary>
        /// <param name="roleName">Name of the role.</param>
        /// <param name="roleId">The role id.</param>
        /// <returns></returns>
        public bool DoesRoleNameExist(string roleName, Guid? roleId)
        {
            Expression<Func<IRole, bool>> expression = role => role.RoleName.ToLower() == roleName.ToLower();
            if (!roleId.HasValue)
            {
                return (_MembershipRepository.FindRoles(expression).Count() > 0);
            }
            else
            {
                IEnumerable<IRole> result = _MembershipRepository.FindRoles(expression);
                if (result.Count() == 1)
                {
                    if (result.First().RoleID == roleId.Value)
                        return false;
                    else return true;
                }
                else return false;
            }
        }

        /// <summary>
        /// Creates the new role.
        /// </summary>
        /// <param name="roleName">Name of the role.</param>
        /// <param name="permissionGuids">The permission guids.</param>
        public void CreateNewRole(string roleName, List<Guid> permissionGuids)
        {
            using (IUnitOfWork scope = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                IRole createdRole = ServiceLocator.Current.GetInstance<IRole>();
                createdRole.RoleID = Guid.NewGuid();
                createdRole.RoleName = roleName;
                foreach (Guid perm in permissionGuids)
                {
                    IPermission rolePerm = ServiceLocator.Current.GetInstance<IPermission>();
                    rolePerm.PermissionID = perm;
                    createdRole.AddPermission(rolePerm);
                }
                _MembershipRepository.CreateRole(createdRole);
                scope.Commit();
            }
        }

        /// <summary>
        /// Updates the role.
        /// </summary>
        /// <param name="roleID">The role ID.</param>
        /// <param name="roleName">Name of the role.</param>
        /// <param name="permissionGuids">The permission guids.</param>
        public void UpdateRole(Guid roleID, string roleName, List<Guid> permissionGuids)
        {
            using (IUnitOfWork scope = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                IRole updatedRole = this._MembershipRepository.GetRoleByID(roleID);

                updatedRole.RoleName = roleName;

                _MembershipRepository.DeleteRolePermissions(updatedRole);
                foreach (Guid perm in permissionGuids)
                {
                    IPermission permission = ServiceLocator.Current.GetInstance<IPermission>();
                    permission.PermissionID = perm;
                    updatedRole.AddPermission(permission);
                }
                scope.Commit();
            }
        }

        /// <summary>
        /// Gets the role by ID.
        /// </summary>
        /// <param name="roleId">The role id.</param>
        /// <returns></returns>
        public IRole GetRoleByID(Guid roleId)
        {
            return _MembershipRepository.GetRoleByID(roleId);
        }

        /// <summary>
        /// Gets all permissions.
        /// </summary>
        /// <returns>
        /// IPermission Array
        /// </returns>
        public IEnumerable<IPermission> GetAllPermissions()
        {
            return _MembershipRepository.GetAllPermissions();
        }
    }
}